package com.zhuiyun.pm.sys.homePage.service.impl;

import com.zhuiyun.pm.sys.homePage.entity.SysChartEntity;
import com.zhuiyun.pm.sys.homePage.entity.dto.BarChartDTO;
import com.zhuiyun.pm.sys.homePage.entity.dto.PieChartDTO;
import com.zhuiyun.pm.sys.homePage.mapper.HomePageMapper;
import com.zhuiyun.pm.sys.homePage.service.HomePageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 *
 * @author: HKY
 * @date: 2023/3/21 16:53
 * @Description: 主页数据统计
 */
@Service
public class HomePageServiceImpl implements HomePageService {

    @Autowired
    private HomePageMapper homePageMapper;

    /**
     *
     * @author: HKY
     * @date: 2023/3/21 16:43
     * @return java.util.List
     * @Description: 柱状图
     */
    @Override
    public Map<String, Object> getBarChart(String sqlStr,String chartType,String operationType) {
        List<BarChartDTO> barChart = homePageMapper.getBarChart(sqlStr);
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        if (barChart != null) {
            if (Integer.valueOf(operationType) != 0){
                SysChartEntity sysChartEntity = new SysChartEntity();
                sysChartEntity.setChartSql(sqlStr);
                sysChartEntity.setChartType(chartType);
                sysChartEntity.setChartCreateTime(new Date());
                //数据显示表插入数据
                homePageMapper.insertUseGeneratedKeys(sysChartEntity);
            }
            String[] strings = new String[barChart.size()];
            int[] ints = new int[barChart.size()];
            for (int i = 0; i < barChart.size(); i++) {
                strings[i] = barChart.get(i).getXAxis();
                ints[i] = barChart.get(i).getSeries();
            }
            stringObjectHashMap.put("xAxis", strings);
            stringObjectHashMap.put("series", ints);
        }
        return stringObjectHashMap;

    }

    /**
     *
     * @author: HKY
     * @date: 2023/3/21 19:38
     * @return java.util.List<java.util.Map < java.lang.String, java.lang.String>>
     * @Description: 饼状图
     */
    @Override
    public List<PieChartDTO> getPieChart(String sqlStr,String operationType) {
        List<PieChartDTO> pieChart = homePageMapper.getPieChart(sqlStr);
        if (pieChart != null) {
            if (Integer.valueOf(operationType) != 0){
                //创建数据显示表实体类并值
                SysChartEntity sysChartEntity = new SysChartEntity();
                sysChartEntity.setChartSql(sqlStr);
                sysChartEntity.setChartType("pieChart");
                sysChartEntity.setChartCreateTime(new Date());
                //数据显示表插入数据
                homePageMapper.insertUseGeneratedKeys(sysChartEntity);
            }
        }
        return pieChart;
    }

    @Override
    public List<SysChartEntity> getChartList() {
        return homePageMapper.selectAll();
    }

    /**
     *
     * @author: HKY
     * @date: 2023/3/24 14:25
     * @param sqlStr
     * @return java.util.HashMap<java.lang.String, java.lang.Object>
     * @Description: 漏斗图
     */
    @Override
    public HashMap<String, Object> getFunnelChart(String sqlStr,String operationType) {
        //根据前端SQL执行查询操作
        List<PieChartDTO> pieChart = homePageMapper.getPieChart(sqlStr);
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        //判断查询结果集不为空
        if (pieChart != null){
            if (Integer.valueOf(operationType) != 0){
                //创建数据显示表实体类并值
                SysChartEntity sysChartEntity = new SysChartEntity();
                sysChartEntity.setChartSql(sqlStr);
                sysChartEntity.setChartType("funnelChart");
                sysChartEntity.setChartCreateTime(new Date());
                //数据显示表插入数据
                homePageMapper.insertUseGeneratedKeys(sysChartEntity);
            }
            //根据结果及大小作为创建的子字符串数组的初始长度
            String[] strings = new String[pieChart.size()];
            //循环集合并去除集合中的name属性根据下标存到字符数组中
            for (int i = 0; i < pieChart.size(); i++) {
                strings[i] = pieChart.get(i).getName();
            }
            //将处理好的数据存入Map集合返回前端
            stringObjectHashMap.put("legend",strings);
            stringObjectHashMap.put("series",pieChart);
        }
        return stringObjectHashMap;
    }

    /**
     *
     * @author: HKY
     * @date: 2023/3/30 14:44
     * @param id
     * @return int
     * @Description: 根据id删除
     */
    @Override
    public int deleteByID(String id) {
        SysChartEntity sysChartEntity = new SysChartEntity();
        sysChartEntity.setId(Integer.valueOf(id));
        return homePageMapper.delete(sysChartEntity);
    }

    /**
     *
     * @author: HKY
     * @date: 2023/3/30 15:16
     * @param sysChartEntity
     * @return int
     * @Description: 修改图表
     */
    @Override
    public int updateChart(SysChartEntity sysChartEntity) {
        return homePageMapper.updateByPrimaryKeySelective(sysChartEntity);
    }
}
